<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Controller that handles all Content Pages
 *
 * @author	kimon.web.designs - Application Framework
 * @package 	CMS
 * @subpackage 	Page Class
 * @category 	Framework
 * @copyright	Copyright (c) 2011, kimon.web.designs
 */

class Page extends Public_Controller
{
    
    private $category_slug;
    private $page_slug;
    private $slugs_found = TRUE;

    /**
     * Constructor method
     * @access public
     * @return void
     */
    public function  __construct() {
        parent::__construct();
        $this->load->model('page_model');
    }

    /**
     * Determines the which page or category to load
     * @access private
     * @return void
     */
    private function set_path_from_uri()
    {
        $sgmnt3 = $this->uri->rsegment(3);
        $sgmnt4 = $this->uri->rsegment(4);

        if (($sgmnt3=='') && ($sgmnt4==''))
        {
            $this->debug('home found');
            // the home page is requested
            $this->page_slug = 'home';
        }
        else
        {
            $slug_ctg_found  = FALSE;
            $slug_page_found = FALSE;

            // find if a category slug exists
            $slug_ctg_found = $this->page_model->ctg_slug_exists($sgmnt3, $this->site_lang);
            if ($slug_ctg_found == TRUE)
            {
                $this->category_slug = $sgmnt3;
                $next_sgmnt = $sgmnt4;
            }
            else
            {
                $next_sgmnt = $sgmnt3;
            }

            // find if a page slug exists
            if ($next_sgmnt!='')
            {
                $slug_page_found = $this->page_model->page_slug_exists($next_sgmnt, $this->site_lang);
                if ($slug_page_found == TRUE)
                {
                    $this->page_slug = $next_sgmnt;
                }
            }
            // determine if anything found
            $this->slugs_found = $slug_ctg_found || $slug_page_found;
        }
    }
    
    /**
     * remap the routing of the controller
     * @access public
     * @return void
     */
    public function _remap($method)
    {
        // find page and category slugs
        $this->set_path_from_uri();

        // route it to the index function
        $this->index();
    }

    /**
     * main routing function
     * @access public
     * @return void
     */
    public function index()
    {
        $data = '';

        if ($this->slugs_found === FALSE)
        {
            $this->_show_404_page('slugs not found');
        }

        if ($this->page_slug=='home')
        {
            $data = $this->_getHomePage();
        }
        else
        {
            if ( isset($this->category_slug) && !isset($this->page_slug) )
            {
                $data = $this->_category($this->category_slug);
            }
            else
            {
                $data = $this->_getPage($this->page_slug);
            }
        }

        $this->template->write('content', $data);
        $this->render();
    }

    /**
     * show home page
     * @access private
     * @return home page content
     */
    private function _getHomePage()
    {
        return $this->_getPage('');
    }

    /**
     * show a page
     * @access private
     * @return page content
     */
    private function _getPage($slug)
    {
        $page_row = $this->page_model->getBySlug($slug, $this->site_lang);

        if (count($page_row)==0)
            $this->_show_404_page($slug);

//        $data = $page_row->text;

        $a['page'] = $page_row;
        $data = $this->load->view('public/layout/single_page', $a, TRUE);

        return $data;
    }

    /**
     * show category page
     * @access private
     * @return category page content
     */
    private function _category($slug='')
    {
        $data = '';

        $pages = $this->page_model->getCategoryPageList($slug, $this->site_lang);

//        foreach ($pages as $page)
//        {
//            $ctg_slug = $this->page_model->getCategorySlug($page->category);
//            $data .= anchor($ctg_slug.'/'.$page->slug, $page->title) . '<br/>';
//        }

        $a['pages'] = $pages;
        $a['ctg_slug'] = $slug;

        $data = $this->load->view('public/layout/blog_page', $a, TRUE);

        return $data;
    }

}

/* End of file pages.php */
/* Location: ./application/core/pages.php */